package org.superheroes.androidclient.gui;

import java.util.ArrayList;

import org.superheroes.androidclient.net.discovery.DiscoveryFinder;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * 
 * @author Jeffrey Goudzwaard
 * @author Davin Terrizi
 * @version 1.0
 *
 */
public class DiscoveryActivity extends ListActivity
{

	private ListView		listView1;
	private DiscoveryFinder	discFinder;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_discovery );

		init();
		this.getListView().setOnItemClickListener( new OnItemClickListener()
		{

			@Override
			public void onItemClick( AdapterView<?> arg0, View arg1, int arg2, long arg3 )
			{

				Intent i = new Intent( DiscoveryActivity.this, ClientActivity.class );

				DiscoveryActivity.this.startActivity( i );
			}
		} );
	}
	
	public ListView getlistView()
	{
		return listView1;
	}

	private void init()
	{

		Thread thread = new Thread( new Runnable()
		{

			@Override
			public void run()
			{
				try
				{
					discFinder = new DiscoveryFinder();
					discFinder.init();
					final ArrayList<Filmpje> filmpjes = discFinder.fillEntries();

					DiscoveryActivity.this.runOnUiThread( new Runnable()
					{

						@Override
						public void run()
						{

							DiscoveryAdapter adapter = new DiscoveryAdapter( DiscoveryActivity.this, filmpjes );

							DiscoveryActivity.this.setListAdapter( adapter );
						}
					} );
				}
				catch ( Exception ex )
				{
					ex.printStackTrace();
				}

			}
		} );

		thread.start();

	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu )
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate( R.menu.discovery, menu );
		return true;
	}

}
